<?php

/**
 * @name BankController
 * @desc 控制器
 */
class BankController extends AdminCommonController
{
    /**
     * 默认动作
     */
    public function indexAction()
    {
        echo "Hello user";
        return false;
    }

    //银行卡列表
    public function listAction(){

        $params = $this->jsonData();

        $row = $params['pageSize'] ?? 12;
        $page = $params['page'] ?? 1;

        $userBankModel = UserbankModel::getSingleton();
        $list = $userBankModel->db
            ->where(['user_id'=> $this->admin_info['user_id'], 'is_delete'=> 0])
            ->order('id asc')
            ->limit(($page - 1) * $row, $row)
            ->select();
        $total = $userBankModel->db
            ->where(['user_id'=> $this->admin_info['user_id'], 'is_delete'=> 0])
            ->count();

        $data = [
            'list' => $list,
            'total' => intval($total),
            'current'   => intval($page),
            'pageSize'  => intval($row),
            'page'  => intval($page),
            'pageNum'   => ceil($total/$row)
        ];

        $this->ajaxReturn(0, '', $data);
    }

    //银行列表
    public function bankidlistAction(){
        $bankModel = BankModel::getSingleton();
        $list = $bankModel->findAll();
        $this->ajaxReturn(0, 'success', $list);
    }

    //添加银行卡
    public function addAction(){
        $params = $this->jsonData();

        $rule = [
            'bank_id'     => 'require',
            'account_name' => 'require',
            'bank_no' => 'require|length:16,19',
            'bank_address'  => 'require',
            'bank_branch_name' => 'require',
        ];
        $msg = [
            'bank_id.require' => '必须选择开户行',
            'account_name.require' => '必须填写开户名',
            'bank_no.require'   => '必须填写银行卡号',
            'bank_no.length'   => '银行卡号不正确',
            'bank_address.require'   => '请选择地区',
            'bank_branch_name.require'   => '必须填写支行名称',
        ];
        $validate = new \Vendor\Kvalidate($rule,$msg);
        if(!$validate->check($params)){
            $this->ajaxReturn(400, $validate->getError());
        }

        $params['user_id'] = $this->admin_info['user_id'];
        $userBankModel = UserbankModel::getSingleton();

        $exist = $userBankModel->find(['user_id'=> $this->admin_info['user_id'], 'bank_no'=> $params['bank_no'], 'is_delete'=> 0]);
        if($exist){
            $this->ajaxReturn(400, '银行卡已存在');
        }

        $count = $userBankModel->count(['user_id'=> $this->admin_info['user_id'], 'is_delete'=> 0]);
        if($count>20){
            $this->ajaxReturn(400, '最多添加20张银行卡');
        }

        $bankInfo = BankModel::getSingleton()->find(['bank_id'=> $params['bank_id']]);
        $params['bank_name'] = $bankInfo['name'];

        $id = $userBankModel->add($params);
        if(!$id){
            $this->ajaxReturn(400, '添加失败');
        }

        $this->ajaxReturn(0, '添加成功');
    }

    //删除银行卡
    public function delAction(){
        $params = $this->jsonData();
        if(!isset($params['id']) || intval($params['id']) ==0){
            $this->ajaxReturn(400, '未获取到数据标识');
        }

        if(!UserbankModel::getSingleton()->update(['id'=> $params['id']], ['is_delete'=> 1])){
            $this->ajaxReturn(400, '删除失败');
        }
        $this->ajaxReturn(0 ,'删除成功');
    }

    //银行卡详情
    public function detailAction(){
        $params = $this->jsonData();
        $detail = UserbankModel::getSingleton()->find(['id'=> $params['id']]);
        $this->ajaxReturn(0, 'success', $detail);
    }
}
